//
//  Save.m
//  PhoenixNews
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "Save.h"

@implementation Save


+ (void)isHaveFolder {
    
    //      1. 找沙盒路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *mineModel = [sandBoxPath stringByAppendingPathComponent:@"Caches"];
    
    //      创建一个文件的管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    
    //      通过管理者调用的方法, 根据路径判断文件夹是否存在
    if (![manager fileExistsAtPath:mineModel]) {
        
        //      创建文件夹
        [manager createDirectoryAtPath:mineModel withIntermediateDirectories:YES attributes:nil error:nil];
        
    }
    NSLog(@"%@", mineModel);
}



+ (BOOL)isHaveModelInTablePlist:(HomePageTableViewModel *)model {
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"table.plist"];
    
    //      反归档
    NSArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    //      遍历
    for (HomePageTableViewModel *temp in modelArr) {
        if ([temp.title isEqualToString:model.title]) {
            return YES;
        }
    }
    return NO;
    
}

+ (BOOL)isHaveModelInCollectionPlist:(HomePageCollectionViewModel *)model {
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"collection.plist"];
    
    //      反归档
    NSArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    //      遍历
    for (HomePageCollectionViewModel *temp in modelArr) {
        if ([temp.pic1 isEqualToString:model.pic1]) {
            return YES;
        }
    }
    return NO;
    
}


+ (BOOL)isHaveModelInScrollPlist:(HomePageScrollViewModel *)model {
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"scroll.plist"];
    
    //      反归档
    NSArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    //      遍历
    for (HomePageScrollViewModel *temp in modelArr) {
        if ([temp.title isEqualToString:model.title]) {
            return YES;
        }
    }
    return NO;
    
}



+ (void)saveModelToTablePlist:(HomePageTableViewModel *)model{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"table.plist"];
    //      直接反归档会覆盖之前的数据, 所以先进行反归档, 然后添加进去之后再进行归档操作
    
    NSMutableArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    if (modelArr == nil) {
        //      如果为空, 先进行初始化
        modelArr = [NSMutableArray array];
    }
    [modelArr addObject:model];
    //      归档
    [NSKeyedArchiver archiveRootObject:modelArr toFile:docPath];
    
}


+ (void)saveModelToCollectionPlist:(HomePageCollectionViewModel *)model {
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"collection.plist"];
    //      直接反归档会覆盖之前的数据, 所以先进行反归档, 然后添加进去之后再进行归档操作
    
    NSMutableArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    if (modelArr == nil) {
        //      如果为空, 先进行初始化
        modelArr = [NSMutableArray array];
    }
    [modelArr addObject:model];
    //      归档
    [NSKeyedArchiver archiveRootObject:modelArr toFile:docPath];
    
}



+ (void)saveModelToScrollPlist:(HomePageScrollViewModel *)model{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"scroll.plist"];
    //      直接反归档会覆盖之前的数据, 所以先进行反归档, 然后添加进去之后再进行归档操作
    
    NSMutableArray *modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    if (modelArr == nil) {
        //      如果为空, 先进行初始化
        modelArr = [NSMutableArray array];
    }
    [modelArr addObject:model];
    //      归档
    [NSKeyedArchiver archiveRootObject:modelArr toFile:docPath];
    
}

+ (void)deleteModel {
    
    NSArray *caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesPath = caches[0];
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL result1 = [manager removeItemAtPath:cachesPath error:nil];
    if (result1) {
        NSLog(@"成功");
    } else {
        NSLog(@"失败");
    }
    NSLog(@"%@", NSHomeDirectory());
}




@end
